<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>地理坐标盒模型过滤器 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/geo-bounding-box.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/geo-bounding-box.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-bounding-box.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-bounding-box.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="geoloc.html">地理位置</a></span>
»
<span class="breadcrumb-link"><a href="geopoints.html">地理坐标点</a></span>
»
<span class="breadcrumb-node">地理坐标盒模型过滤器</span>
</div>
<div class="navheader">
<span class="prev">
<a href="filter-by-geopoint.html">« 通过地理坐标点过滤</a>
</span>
<span class="next">
<a href="geo-distance.html">地理距离过滤器 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="geo-bounding-box"></a>地理坐标盒模型过滤器 (geo_bounding_box Filter)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/310_Geopoints/32_Bounding_box.asciidoc">edit</a>
</h2>
</div></div></div>
<p>这(<code class="literal">geo_bounding_box</code>)是目前为止最有效的地理坐标过滤器了，因为它计算起来非常简单。  你指定一个矩形的<code class="literal">顶部</code>、<code class="literal">底部</code>、<code class="literal">左边界</code>、<code class="literal">右边界</code>坐标，然后过滤器只需判断坐标的经度是否在左右边界之间，纬度是否在上下边界之间：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /attractions/restaurant/_search
{
  "query": {
    "filtered": {
      "filter": {
        "geo_bounding_box": {
          "location": { <a id="CO227-1"></a><i class="conum" data-value="1"></i>
            "top_left": {
              "lat":  40.8,
              "lon": -74.0
            },
            "bottom_right": {
              "lat":  40.7,
              "lon": -73.0
            }
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO227-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>这些坐标也可以用 <code class="literal">bottom_left</code> 和 <code class="literal">top_right</code> 来表示。<span style="color:#666;">(球面上任意两个经纬度都不同的点就能确定一个矩形)</span></p>
</td>
</tr>
</table>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="optimize-bounding-box"></a>优化盒模型<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/310_Geopoints/32_Bounding_box.asciidoc">edit</a>
</h3>
</div></div></div>

<p><code class="literal">geo_bounding_box</code>不需要把所有坐标点都加载到内存里。因为它要做的只是简单判断<code class="literal">lat</code>和<code class="literal">lon</code>值是否在给定的范围内，它可以用倒排索引执行一个(性能杠杠的)范围(<code class="literal">range</code>)过滤。</p>

<p>要使用这种优化方式，需要把<code class="literal">geo_point</code>字段用<code class="literal">lat</code>和<code class="literal">lon</code> 的方式分别映射到索引中：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /attractions
{
  "mappings": {
    "restaurant": {
      "properties": {
        "name": {
          "type": "string"
        },
        "location": {
          "type":    "geo_point",
          "lat_lon": true <a id="CO228-1"></a><i class="conum" data-value="1"></i>
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO228-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">location.lat</code> 和 <code class="literal">location.lon</code> 字段将被分别索引。它们可以被用于检索，但是不会在检索结果中返回。</p>
</td>
</tr>
</table>
</div>
<p>然后，查询时你需要告诉 Elasticesearch 使用已索引的<code class="literal">lat</code>和<code class="literal">lon</code>：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /attractions/restaurant/_search
{
  "query": {
    "filtered": {
      "filter": {
        "geo_bounding_box": {
          "type":    "indexed", <a id="CO229-1"></a><i class="conum" data-value="1"></i>
          "location": {
            "top_left": {
              "lat":  40.8,
              "lon": -74.0
            },
            "bottom_right": {
              "lat":  40.7,
              "lon":  -73.0
            }
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO229-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>设置<code class="literal">type</code>参数值为<code class="literal">indexed</code>（替代默认值<code class="literal">memory</code>）来明确告诉 Elasticsearch 对这个过滤器使用倒排索引。</p>
</td>
</tr>
</table>
</div>
<div class="caution admon">
<div class="icon"></div>
<div class="admon_content">
<p><code class="literal">geo_point</code> 类型的字段可以包含多个地理坐标点，但是针对经度纬度分别索引的这种优化方式只对包含单个坐标点的字段有效。</p>
</div>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="filter-by-geopoint.html">« 通过地理坐标点过滤</a>
</span>
<span class="next">
<a href="geo-distance.html">地理距离过滤器 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>